#include "./include/tcp.server.h"

int main(int argc, char **argv)
{
    if (argc != 3)
    {
        printf("Please input ip or port!\n");
        goto err;
    }
    int sockfd;
    char ip[20] = {0};
    char tmp[ARR];
    char tmp_passwd[ARR];
    int num = 0;
    int r_len;
    int port = atoi(argv[2]);

    strcpy(ip, argv[1]);

    struct sockaddr_in s_addr;
    online user;

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        perror("socket error!");
        goto err;
    }

    s_addr.sin_family = AF_INET;
    s_addr.sin_port = htons(port);
    s_addr.sin_addr.s_addr = inet_addr(ip);

    if (connect(sockfd, (struct sockaddr *)&s_addr, sizeof(struct sockaddr_in)) == -1)
    {
        perror("connect error!");
        goto err;
    }

    while (1)
    {
        login_opt();
        memset(tmp, 0, sizeof(tmp));
        printf("\033[1;33;45m\t请输入要进行的操作: ");
        scanf("%s", tmp);
        num = change(tmp);
        if (REGISTER == num)
        {
            // user_reg();
            memset(&user, 0, sizeof(online));
            user.signal = 1;
            printf("请输入要注册的用户名: ");
            scanf("%s", user.use);
            printf("请输入要注册的密码: ");
            getchar();
            private_input(tmp_passwd);
            strcpy(user.passwd, rc4_encrypt(tmp_passwd));
            send(sockfd, &user, sizeof(online), 0);

            memset(&user, 0, sizeof(online));
            recv(sockfd, &user, sizeof(online), 0);
            if (REGISTER == user.signal)
            {
                printf("用户注册成功!\n");
                getchar();
                getchar();
            }
            else if (ERROR == user.signal)
            {
                printf("用户已存在，请重新注册!\n");
                getchar();
                getchar();
            }
        }
        else if (LOGIN == num)
        {
        on:
            memset(&user, 0, sizeof(online));
            user.signal = 2;
            printf("请输入要登录的用户: ");
            scanf("%s", user.use);
            printf("请输入要登录的用户名密码: ");
            getchar();
            private_input(tmp_passwd);
            strcpy(user.passwd, rc4_encrypt(tmp_passwd));
            send(sockfd, &user, sizeof(online), 0);

            memset(&user, 0, sizeof(online));
            recv(sockfd, &user, sizeof(online), 0);
            if (LOGIN == user.signal)
            {

                printf("\n用户登录成功!\n");
                sleep(1);
                break;
            }
            else if (ALR_LOG == user.signal)
            {
                printf("\n该用户已登录，即将返回主界面!\n");
                sleep(2);
                continue;
            }
            else if (ERROR == user.signal)
            {
                printf("\n登录失败，用户不存在或密码错误！\n");
                getchar();
                login_opt();
                // getchar();
                goto on;
            }
        }
        else if (EXIT == num)
        {
            printf("！将在2S后退出系统！\n");
            sleep(2);
            return 0;
        }
    }
    while (1)
    {
        menu();
        printf("请选择操作:\t\n");
        printf("\tADD /SEE /SEARCH /DEL /UPDATE / EXIT :  ");
        memset(tmp, 0, sizeof(tmp));
        scanf("%s", tmp);
        num = change(tmp);
        switch (num)
        {
        case ADD:
        {
            // system("clear");
        on1:
            memset(&user, 0, sizeof(online));
            user.signal = ADD;
            printf("请输入要添加的联系人姓名： ");
            scanf("%s", user.name);
            printf("请输入要添加的联系人年龄： ");
            scanf("%d", &user.age);
            printf("请输入要添加的联系人电话： ");
            scanf("%s", user.tel);
            printf("请输入要添加的联系人地址： ");
            scanf("%s", user.addr);
            printf("请输入要添加的联系人邮箱： ");
            scanf("%s", user.mailbox);
            send(sockfd, &user, sizeof(online), 0);

            memset(&user, 0, sizeof(online));
            recv(sockfd, &user, sizeof(online), 0);
            if (ALR_LOG == user.signal)
            {
                printf("该联系人已存在，请勿重复添加！！\n");
                getchar();
                getchar();
                continue;
            }
            else
            {
                printf("添加联系人成功!,是否继续添加联系人(Y/N)?\n");
                scanf("%s", tmp);
                num = change(tmp);
                printf("num = %d\n", num);
                if (Y == num)
                {
                    goto on1;
                }
                else if (N == num)
                {
                    continue;
                }
            }
            break;
        }
        case SEE:
        {
            memset(&user, 0, sizeof(online));
            user.signal = SEE;
            send(sockfd, &user, sizeof(online), 0);
            system("clear");
            printf("当前联系人列表为： \n");
            while (1)
            {
                memset(&user, 0, sizeof(online));
                r_len = recv(sockfd, &user, sizeof(online), 0);
                if (FIND == user.signal)
                {
                    if (r_len > 0)
                    {
                        printf("联系人编号： %d\n", user.id);
                        printf("联系人姓名： %s\n", user.name);
                        printf("联系人年龄： %d\n", user.age);
                        printf("联系人电话： %s\n", user.tel);
                        printf("联系人地址： %s\n", user.addr);
                        printf("联系人邮箱： %s\n", user.mailbox);
                        printf("\n");
                    }
                }
                else if(EXIT == user.signal)
                {
                    break;
                }
            }
            getchar();
            getchar();
            break;
        }
        case SEARCH:
        {
        on2:
            system("clear");
            memset(&user, 0, sizeof(online));
            user.signal = SEARCH;
            printf("请输入要查找的联系人姓名： ");
            scanf("%s", user.name);
            send(sockfd, &user, sizeof(online), 0);

            memset(&user, 0, sizeof(online));
            recv(sockfd, &user, sizeof(online), 0);
            if (FIND == user.signal)
            {
                printf("查询成功！！\n");
                printf("联系人信息为： \n");
                printf("姓名：%s\n", user.name);
                printf("年龄：%d\n", user.age);
                printf("电话：%s\n", user.tel);
                printf("地址：%s\n", user.addr);
                printf("邮箱：%s\n", user.mailbox);
                printf("\n");
                printf("是否继续查询联系人?(Y/N)\n");
                scanf("%s", tmp);
                num = change(tmp);
                if (Y == num)
                {
                    goto on2;
                }
                else if (N == num)
                {
                    continue;
                }
            }
            else if (ERROR == user.signal)
            {
                printf("查询失败！不存在该用户信息！\n");
                getchar();
                getchar();
            }
            break;
        }
        case DELETE:
        {
        on3:
            memset(&user, 0, sizeof(online));
            user.signal = DELETE;
            printf("请输入要删除的联系人姓名： ");
            scanf("%s", tmp);
            strcpy(user.name, tmp);
            send(sockfd, &user, sizeof(online), 0);

            memset(&user, 0, sizeof(online));
            recv(sockfd, &user, sizeof(online), 0);
            if (DELETE == user.signal)
            {
                printf("成功删除联系人: %s\n!!!", tmp);
                printf("是否继续删除联系人？(Y/N) : ");
                scanf("%s", tmp);
                num = change(tmp);
                if (Y == num)
                {
                    goto on3;
                }
                else if (N == num)
                {
                    break;
                }
            }
            break;
        }
        case UPDATE:
        {
        on4:
            // system("clear");
            memset(&user, 0, sizeof(online));
            user.signal = UPDATE;
            printf("请输入要修改的联系人姓名： ");
            scanf("%s", user.name);
            printf("请输入要修改的联系人年龄： ");
            scanf("%d", &user.age);
            printf("请输入要修改的联系人电话： ");
            scanf("%s", user.tel);
            printf("请输入要修改的联系人地址： ");
            scanf("%s", user.addr);
            printf("请输入要修改的联系人邮箱： ");
            scanf("%s", user.mailbox);
            printf("signal clint = %d\n", user.signal);
            send(sockfd, &user, sizeof(online), 0);

            memset(&user, 0, sizeof(online));
            recv(sockfd, &user, sizeof(online), 0);
            if (UPDATE == user.signal)
            {
                printf("修改联系人信息成功！是否继续修改？(Y/N):");
                scanf("%s", tmp);
                num = change(tmp);
                if (Y == num)
                {
                    goto on4;
                }
                else if (N == num)
                {
                    break;
                }
            }
            break;
        }
        case EXIT:
        {
            printf("！将在2S后退出系统！\n");
            sleep(2);
            goto err;
        }
        default:
        {
            printf("非法输入，已强制退出！\n");
            break;
        }
        }
    }

    close(sockfd);
err:
    return 0;
}
